"use strict";

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['elfinder'], factory);
    } else if (typeof exports !== 'undefined') {
        module.exports = factory(require('elfinder'));
    } else {
        factory(root.elFinder);
    }
}(this, function (elFinder) {
    elFinder.prototype.commands.editoffice = function () {
        var officemines = ['application/msword',
            'application/msword',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
            'application/vnd.ms-word.document.macroEnabled.12',
            'application/vnd.ms-word.template.macroEnabled.12',
            'application/vnd.ms-excel',
            'application/vnd.ms-excel',
            'application/vnd.ms-excel',
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
            'application/vnd.ms-excel.sheet.macroEnabled.12',
            'application/vnd.ms-excel.template.macroEnabled.12',
            'application/vnd.ms-excel.addin.macroEnabled.12',
            'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
            'application/vnd.ms-powerpoint',
            'application/vnd.ms-powerpoint',
            'application/vnd.ms-powerpoint',
            'application/vnd.ms-powerpoint',
            'application/vnd.openxmlformats-officedocument.presentationml.presentation',
            'application/vnd.openxmlformats-officedocument.presentationml.template',
            'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
            'application/vnd.ms-powerpoint.addin.macroEnabled.12',
            'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
            'application/vnd.ms-powerpoint.template.macroEnabled.12',
            'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'];
        var extstype = [
            'docx',
            'xlsx',
            'pptx'
        ]
        var self = this,
            fm = this.fm,
            mimes = officemines,
            rtrim = function (str) {
                return str.replace(/\s+$/, '');
            };

        this.extMatch = function (fileName, editorExts) {
            if (!editorExts || !editorExts.length) {
                return true;
            }
            var ext = fileName.replace(/^.+\.([^.]+)|(.+)$/, '$1$2').toLowerCase(),
                i, l;
            l = editorExts.length;
            for (i = 0; i < l; i++) {
                if (ext === editorExts[i].toLowerCase()) {
                    return true;
                }
            }
            return false;
        };
        /**
         * Return files acceptable to edit
         *
         * @param  Array  files hashes
         * @return Array
         **/
        this.filter = function (files) {
            return $.map(files, function (file) {
                return (file.mime.indexOf('application/') === 0 || $.inArray(file.mime, mimes) !== -1)
                && file.mime.indexOf('text/rtf')
                && (!self.onlyMimes.length || $.inArray(file.mime, self.onlyMimes) !== -1)
                && file.read && file.write ? file : null;
            });
        };

        this.init = function () {
            this.onlyMimes = mimes || [];
        };

        this.getstate = function (sel) {
            var sel = this.files(sel),
                cnt = sel.length;
            return cnt == 1 && this.extMatch(sel[0].name, extstype) && this.filter(sel).length == cnt ? 0 : -1;
        };

        this.exec = function (hashes) {
            //implement what the custom command should do here
            var fm = this.fm,
                files = this.files(hashes),
                cnt = files.length,
                thash = (typeof opts == 'object') ? opts.thash : false,
                opts = this.options,
                url,
                file = files[cnt - 1];

            if (!file.write) {
                alert("该文件已被设置为不能编辑");
            }
            url = this.openOfficeEditUrl(file);
            window.open(url);
        };
        /**
         * Return file url for open in elFinder
         *
         * @param  String  file hash
         * @param  Boolean for download link
         * @return String
         */
        this.openOfficeEditUrl = function (file) {
            var url = '';

            if (!file || !file.read) {
                return '';
            }

            url = "/a/ooffice/editor";
            url = url + (url.indexOf('?') === -1 ? '?' : '&')
                + '&target=' + file.hash;
            $.each(this.options.customData, function (key, val) {
                url += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(val);
            });

            return url;
        };
    }
}));
